﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Web.Model;
using Portal.Cms.Model;
using Portal.Web.Utility;
using Portal.Cms.Bussiness;
using Portal.Cms.Utility;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class RelationTypeRenderBll
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Tham số truy cập không hợp lệ!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null)
                {
                    throw new Exception("Tham số truy cập không hợp lệ!");
                }
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, gid, WebSessionUtility.GetCurrentLoginUser().UserId, true);

                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientAdd(DataType)\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Cms.Render.RelationTypeRenderBll.DrawAddForm(SiteId, DataType).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtSubject').focus();\r\n"+
                   "    }\r\n" +

                   "    function ClientUpdate(DataType,RelationTypeId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Cms.Render.RelationTypeRenderBll.DrawUpdateForm(SiteId, DataType, RelationTypeId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtSubject').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientInputData(DataType,RelationTypeId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(DataType=='document')\r\n"+
                   "        {\r\n"+
                   "            window.open('DocumentMan/Home.aspx?gid='+RelationTypeId,'_self');\r\n" +
                   "        }\r\n"+
                   "        else\r\n"+
                   "        {\r\n"+
                   "            window.open('InputData.aspx?gid='+RelationTypeId,'_self');\r\n"+
                   "        }\r\n"+
                   "    }\r\n" +


                   "    function ClientBack()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divActionForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientAddRelationType(GroupDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        subject = document.getElementById('txtSubject').value;\r\n" +
                   "        SortIndex = document.getElementById('txtSortIndex').value;\r\n" +
                   "        InOut = document.getElementById('drpSelectType').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.RelationTypeRenderBll.ServerSideAdd(SiteId,GroupDataId, subject, SortIndex, InOut).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n"+
                   "        {\r\n"+
                   "            alert(AjaxOut.InfoMessage);\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n"+
                   "    }\r\n" +


                   "    function ClientDelete(GroupDataId,RelationTypeId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n"+
                   "        AjaxOut = Portal.Cms.Render.RelationTypeRenderBll.ServerSideDelete(SiteId,GroupDataId, RelationTypeId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+

                   "    function ClientSaveRelationType(GroupDataId,RelationTypeId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        subject = document.getElementById('txtSubject').value;\r\n" +
                   "        SortIndex = document.getElementById('txtSortIndex').value;\r\n" +
                   "        InOut = document.getElementById('drpSelectType').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.RelationTypeRenderBll.ServerSideSave(SiteId,GroupDataId, RelationTypeId, subject, SortIndex, InOut).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n"+

                   "    function SearchData()\r\n"+
                   "    {\r\n"+
                   "        document.getElementById('divSeachDataForm').style.display='block';\r\n"+
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "    }\r\n"+

                   "    function ClientBackFromSearchForm()\r\n"+
                   "    {\r\n"+
                   "        document.getElementById('divSeachDataForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n"+

                   "    function ClientExecuteSearchData()\r\n"+
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        DataType = document.getElementById('drpSelectDataType').value;\r\n"+
                   "        SearchSubject = document.getElementById('txtSearchSubject').value;\r\n"+
                   "        SearchDescription = document.getElementById('txtSearchDescription').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.RelationTypeRenderBll.ServerSideSearchData(SiteId,DataType, SearchSubject, SearchDescription).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divSearchResult').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+
                   "</script>\r\n" +
                   
                   "<div id=divActionForm class=CssActionForm></div>\r\n" +
                   "<div id=divListForm class=tabcontent>\r\n" +
                        ServerSideReading(SiteId,gid).HtmlContent+
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (OGroupData == null)
                {
                    throw new Exception("Tham số truy cập không hợp lệ!");
                }

                RelationTypeCls[] RelationType = RelationTypeProcessBll.ReadingRelationType(SiteId, GroupDataId);
                
                string Html =
                   "        <h3>" + OGroupData.GroupDataName + " (" + RelationType.Length + ")<input type=button class=button value=\"Quay lại\" onclick=\"javascript:window.open('DocumentInputData.aspx?gid=" + GroupDataId + "','_self');\" style=\"float:right;margin-right:10px;\"><input type=button class=button value=\"Thêm mới\" onclick=\"javascript:ClientAdd('" + GroupDataId + "');\" style=\"float:right;margin-right:10px;\"></h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th>#</th>\r\n" +
                   "                    <th>Tiêu đề</th>\r\n" +
                   "                    <th>Kiểu quan hệ</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < RelationType.Length; iIndex++)
                {
                    string RelType = "Một chiều";
                    if (RelationType[iIndex].InOut == 1)
                    {
                        RelType = "Hai chiều";
                    }

                    Html +=
                       "                <tr>\r\n" +
                       "                    <td style=\"width:30px;vertical-align:right;\">" + (iIndex + 1) + "</td>\r\n" +
                       "                    <td>" + RelationType[iIndex].RelationTypeName + "</td>\r\n" +
                       "                    <td>" + RelType + "</td>\r\n" +
                       "                    <td style=\"width:10px;\"><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + GroupDataId + "','" + RelationType[iIndex].RelationTypeId + "');\"></td>\r\n" +
                       "                    <td style=\"width:10px;\"><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + GroupDataId + "','" + RelationType[iIndex].RelationTypeId + "');\"></td>\r\n" +
                       "                </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string SelectType = "<select id=drpSelectType>\r\n" +
                                    "   <option value=\"0\">Quan hệ một chiều</option>\r\n" +
                                    "   <option value=\"1\">Quan hệ hai chiều</option>\r\n" +
                                    "</select>\r\n";

           

                string Html =
                   "  <h3>Thêm mới dữ liệu</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Tiêu đề:</td>\r\n"+
                   "        <td class=c2><input id=txtSubject class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n"+
                   "    <tr>\r\n" +
                   "        <td class=c1>Thứ tự:</td>\r\n" +
                   "        <td class=c2><input id=txtSortIndex class=textbox type=textbox value=\"1\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Kiểu:</td>\r\n" +
                   "        <td class=c2>"+SelectType+"</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1></td>\r\n" +
                   "        <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:ClientAddRelationType('"+GroupDataId+"');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId, string GroupDataId,string RelationTypeId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string SelectType = "<select id=drpSelectType>\r\n" +
                                 "   <option value=\"0\">Quan hệ một chiều</option>\r\n" +
                                 "   <option value=\"1\">Quan hệ hai chiều</option>\r\n" +
                                 "</select>\r\n";
           
                RelationTypeCls ORelationType = RelationTypeProcessBll.CreateRelationTypeModel(SiteId, RelationTypeId);
                if (ORelationType.InOut == 1)
                {
                    SelectType = "<select id=drpSelectType>\r\n" +
                                 "   <option value=\"0\">Quan hệ một chiều</option>\r\n" +
                                 "   <option selected value=\"1\">Quan hệ hai chiều</option>\r\n" +
                                 "</select>\r\n";
                }
                string Html =
                   "  <h3>Sửa nhóm dữ liệu</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Tiêu đề:</td>\r\n" +
                   "        <td class=c2><input id=txtSubject class=textbox type=textbox value=\"" + ORelationType.RelationTypeName + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Thứ tự:</td>\r\n" +
                   "        <td class=c2><input id=txtSortIndex class=textbox type=textbox value=\"" + ORelationType.SortIndex + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1>Kiểu:</td>\r\n" +
                   "        <td class=c2>" + SelectType + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=c1></td>\r\n" +
                   "        <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:ClientSaveRelationType('" + GroupDataId + "','" + RelationTypeId + "');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(string SiteId, string GroupDataId, string Subject, string SortIndex,string InOut)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa nhập tiêu đề nhóm dữ liệu!");
                RelationTypeProcessBll.AddRelationType(SiteId, GroupDataId, Subject, SortIndex, InOut);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(string SiteId, string GroupDataId, string RelationTypeId, string Subject, string SortIndex,string InOut)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa nhập tiêu đề nhóm dữ liệu!");
                RelationTypeProcessBll.SaveRelationType(SiteId, RelationTypeId, Subject, SortIndex, InOut);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string DataType,string RelationTypeId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                RelationTypeProcessBll.DeleteRelationType(SiteId, RelationTypeId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, DataType).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }
    }
}
